﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MentorBackEnd.Model;
using System.Data.SqlClient;
using System.Data;

namespace MentorBackEnd.DA
{
    public class MentorMatchTagDA
    {
        public static void InsertMentorTagsForUsers(int userID, string tagsCSL)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MentorTagInsert]");
                cmd.Parameters.Add(new SqlParameter("@UserID", userID));
                cmd.Parameters.Add(new SqlParameter("@TagsCSL", tagsCSL));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }


        public static void UpdateMentorTagsForUsers(int userID, string tagsCSL)
        {
             //insert and update are the same logic -delete and add new.
             InsertMentorTagsForUsers( userID, tagsCSL);
             return;
        }

        public static List<MentorMatchTag> GetUserMentorTags(int userID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MentorMatchTag> collection = new List<MentorMatchTag>();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MentorTagSelect]");
                cmd.Parameters.Add(new SqlParameter("@UserID", userID));
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MentorMatchTag dataModel = new MentorMatchTag(reader);
                    collection.Add(dataModel);

                }

                if (!reader.IsClosed)
                    reader.Close();

                if (collection.Count > 0)
                    return collection;
                else
                    return null;
            }

            catch { throw; }
            finally { conn.Close(); }
        }
    }
}